package com.xnf.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xnf.article.feigh.BehaviorFeign;
import com.xnf.article.feigh.UserFeign;
import com.xnf.article.mapper.ApArticleConfigMapper;
import com.xnf.article.mapper.ApArticleContentMapper;
import com.xnf.article.mapper.ApCollectionMapper;
import com.xnf.article.mapper.AuthorMapper;
import com.xnf.article.service.ArticleInfoService;
import com.xnf.model.article.dtos.ArticleInfoDto;
import com.xnf.model.article.pojos.ApArticleConfig;
import com.xnf.model.article.pojos.ApArticleContent;
import com.xnf.model.article.pojos.ApCollection;
import com.xnf.model.behavior.pojos.ApBehaviorEntry;
import com.xnf.model.behavior.pojos.ApLikesBehavior;
import com.xnf.model.behavior.pojos.ApUnlikesBehavior;
import com.xnf.model.common.dtos.ResponseResult;
import com.xnf.model.common.enums.AppHttpCodeEnum;
import com.xnf.model.user.pojos.ApUser;
import com.xnf.model.user.pojos.ApUserFollow;
import com.xnf.utils.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xnf on 2022/3/8.
 */
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Autowired
    private ApCollectionMapper apCollectionMapper;
    @Autowired
    private AuthorMapper authorMapper;

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        Map<String, Object> resultMap = new HashMap<>();
        //1.检查参数
        if (dto == null || dto.getArticleId() == null) {
            System.out.println("dto == null");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询文章的配置
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, dto.getArticleId()));
        if (apArticleConfig == null) {
            System.out.println("apArticleConfig == null");
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.查询文章的内容
        if (!apArticleConfig.getIsDelete() && !apArticleConfig.getIsDown()) {
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getArticleId()));
            resultMap.put("content", apArticleContent);
        }
        resultMap.put("config", apArticleConfig);
        //4.结果返回
        return ResponseResult.okResult(resultMap);
    }

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        ApUser apUser = AppThreadLocalUtils.get();
        Integer userId = null;

        ApBehaviorEntry apBehaviorEntry = null;
        if (apUser != null) {
            userId = apUser.getId();
            apBehaviorEntry = behaviorFeign.findByUserIdOrEntryId(userId, null);
        }else{
            System.out.println("userId " + userId);
            System.out.println(dto.getEquipmentId());
            apBehaviorEntry = behaviorFeign.findByUserIdOrEntryId(userId, dto.getEquipmentId().longValue());
        }
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer entryId = apBehaviorEntry.getEntryId();
        boolean isUnlike = false, isLike = false, isCollection = false, isFollow = false;
        if (apUser != null && userId != null){
            //3.查询不喜欢行为
            ApUnlikesBehavior apUnlikesBehavior = behaviorFeign.findUnLikeByArticleIdAndEntryId(entryId, dto.getArticleId());
            if (apUnlikesBehavior != null && apUnlikesBehavior.getType() == ApUnlikesBehavior.Type.UNLIKE.getCode()) {
                isUnlike = true;
            }
            //4.查询点赞行为
            ApLikesBehavior apLikesBehavior = behaviorFeign.
                    findLikeByArticleIdAndEntryId(entryId, dto.getArticleId(), ApLikesBehavior.Type.ARTICLE.getCode());
            if (apLikesBehavior != null && apLikesBehavior.getOperation() == ApLikesBehavior.Operation.LIKE.getCode()) {
                isLike = true;
            }
            //5.查询收藏行为
            ApCollection apCollection = apCollectionMapper.selectOne(Wrappers.<ApCollection>lambdaQuery()
                    .eq(ApCollection::getArticleId, dto.getArticleId())
                    .eq(ApCollection::getEntryId, entryId)
                    .eq(ApCollection::getType, ApCollection.Type.ARTICLE.getCode()));
            if (apCollection != null) {
                isCollection = true;
            }
            //6.查询是否关注
            if (apUser != null) {
                ApUserFollow apUserFollow = userFeign.findByUserIdAndFollowId(apUser.getId(), dto.getAuthorId());
                if (apUserFollow != null) {
                    isFollow = true;
                }
            }
        }
        //7.结果返回  {"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("isfollow",isFollow);
        resultMap.put("islike",isLike);
        resultMap.put("isunlike",isUnlike);
        resultMap.put("iscollection",isCollection);
        return ResponseResult.okResult(resultMap);
    }
}
